package com.example.logindb.controller;



import com.example.logindb.dto.RelationDTo;
import com.example.logindb.dto.ResponseMessage;
import com.example.logindb.dto.UserDTO;
import com.example.logindb.entity.Feedback;
import com.example.logindb.entity.Help;
import com.example.logindb.entity.Relation;
import com.example.logindb.entity.User;
import com.example.logindb.repository.HelpRepository;
import com.example.logindb.sevices.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;


@RestController
@RequestMapping("/relation")
public class RelationController {

    private HelpRepository helpRepository;
    private RelationService relationService;
    private UserService userService;
    private HelpService helpService;
    private FeedbackService feedbackService;
    @Autowired
    public RelationController(RelationServiceImpl relationService, UserService userService, HelpService helpService, FeedbackService feedbackService, HelpRepository helpRepository) {
        this.relationService = relationService;
        this.userService = userService;
        this.helpService = helpService;
        this.feedbackService = feedbackService;
        this.helpRepository = helpRepository;
    }


    //增加
    @PostMapping
    public ResponseMessage<Relation> add_relation(@RequestBody Relation relation)
    {
        System.out.println("正在增加一条绑定信息");
        Relation relationNew=relationService.addRelation(relation);
        return ResponseMessage.success(relationNew);
    }

    //亲属查询盲人
    @GetMapping("/getblindid")
    public ResponseMessage get_relation(@RequestParam Integer guardian_id)
    {
        Relation relation = relationService.getRelation(guardian_id);
        return ResponseMessage.success(relation);
    }

    //修改,没用上
    @PostMapping("/edit")
    public ResponseMessage edit_relation(@RequestBody Relation relation)
    {
        System.out.println("修改中");
        Relation relationnew=relationService.edit(relation);
        return ResponseMessage.success(relationnew);
    }

    //根据盲人id查询亲属信息
    @GetMapping("/getginfo")
    public ResponseMessage<UserDTO> get_guardian_by_blindid(
            @RequestParam Integer blind_id)
    {
        Relation relation = relationService.getRelationByBid(blind_id);
        if (relation == null)
            return ResponseMessage.fail(null);
        int gId = relation.getGuardianid();
        User ifPresent = userService.ifpresent(gId);
        if(ifPresent==null)
            return ResponseMessage.fail(null);
        else
        {
            UserDTO guser = userService.findById(gId);
            guser.setPassword(null);
            return ResponseMessage.success(guser);
        }
    }

    //亲属查盲人求助记录
    @GetMapping("/gethelpinfo")
    public ResponseMessage<List<Help>> get_help_info(
            @RequestParam Integer bid)
    {
        List<Help> helps = helpService.findByBid(bid);
        if(!helps.isEmpty())
            return ResponseMessage.success(helps);
        return ResponseMessage.fail(null);
    }

    @GetMapping("/gethelpinfow")
    public List<Map<String, Object>> getHelpInfo(
            @RequestParam Integer bid
    ) {
        List<Help> helpList = helpService.findByBidDone(bid);

        // 将 Help 对象列表转换为 Map<String, Object> 列表

        return helpList.stream()
                .map(help -> {
                    return Map.<String, Object>of(
                            "aid", help.getAid(),
                            "bid", help.getBid(),
                            "vid", help.getVid(),
                            "stime", help.getStime(),
                            "etime", help.getEtime(),
                            "btime", help.getBtime(),
                            "lat", help.getLat(),
                            "lon", help.getLon(),
                            "status", help.getStatus(),
                            "address", help.getAddress()
                    );
                })
                .collect(Collectors.toList());
    }

    @GetMapping("/gethelpinfov")
    public List<Map<String, Object>> getHelpInfov(
            @RequestParam Integer vid
    ) {
        List<Help> helpList = helpService.findByBVidDone(vid);

        // 将 Help 对象列表转换为 Map<String, Object> 列表

        return helpList.stream()
                .map(help -> {
                    return Map.<String, Object>of(
                            "aid", help.getAid(),
                            "bid", help.getBid(),
                            "vid", help.getVid(),
                            "stime", help.getStime(),
                            "etime", help.getEtime(),
                            "btime", help.getBtime(),
                            "lat", help.getLat(),
                            "lon", help.getLon(),
                            "status", help.getStatus(),
                            "address", help.getAddress()
                    );
                })
                .collect(Collectors.toList());
    }
//获取所有求助点位置，画热力图
    @GetMapping("/getallhelpinfo")
    public List<Map<String, Object>> getAllHelpInfo(
    ) {
        List<Help> helpList = helpRepository.findAllByStatus(2);

        // 将 Help 对象列表转换为 Map<String, Object> 列表

        return helpList.stream()
                .map(help -> {
                    return Map.<String, Object>of(
                            "aid", help.getAid(),
                            "bid", help.getBid(),
                            "vid", help.getVid(),
                            "stime", help.getStime(),
                            "etime", help.getEtime(),
                            "btime", help.getBtime(),
                            "lat", help.getLat(),
                            "lon", help.getLon(),
                            "status", help.getStatus(),
                            "address", help.getAddress()
                    );
                })
                .collect(Collectors.toList());
    }


    //亲属根据志愿者id查志愿者信息
    @GetMapping("/getvinfo")
    public ResponseMessage<UserDTO> get_volunteer_by_vid(
            @RequestParam Integer vid)
    {
       UserDTO vuser = userService.findById(vid);
       if(vuser == null)
           return ResponseMessage.fail(null);
       else {
           vuser.setPassword(null);
           return ResponseMessage.success(vuser);
       }
    }

    //反馈
    @PostMapping("/feedback")
    public ResponseMessage feedback(
            @RequestBody Feedback feedback
            )
    {
        feedback.setTime(Timestamp.valueOf(LocalDateTime.now()));
        feedbackService.save(feedback);
        return ResponseMessage.success("success!");
    }

}
